package cn.xiaomubiao.oschine.fragment.myinfofragment.collection;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import cn.xiaomubiao.oschine.activity.MyInfoBaseActivity;
import cn.xiaomubiao.oschine.adapter.myinfo.MyInfoBaseListAdapter;
import cn.xiaomubiao.oschine.adapter.myinfo.MyInfoCollectionCommonAdapter;
import cn.xiaomubiao.oschine.bean.FavoriteList;
import cn.xiaomubiao.oschine.contants.Host;
import cn.xiaomubiao.oschine.fragment.myinfofragment.MyInfoBaseListFragment;
import cn.xiaomubiao.oschine.util.HttpUtil;
import cn.xiaomubiao.oschine.util.ToastUtils;

/**
 * @author
 * @version 1.0
 * @date 2017/4/2
 */

public abstract class MyInfoCollectionCommonFragment extends MyInfoBaseListFragment {

    public FavoriteList bean;

    public MyInfoCollectionCommonAdapter mAdapter;

    @Override
    protected MyInfoBaseListAdapter getListAdapter() {
        mAdapter = new MyInfoCollectionCommonAdapter();
        mAdapter.setOnItemClickListener(new MyInfoBaseListAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View view, int position) {
                int idString = bean.getFavoritelist().get(position).getId();
                Intent intent = new Intent(getActivity(), MyInfoBaseActivity.class);
                
                if (idString == 19230){
                    intent.putExtra("idString", "actionbarsherlock");
                }else if (idString == 35669){
                    intent.putExtra("idString", "greendroid");
                }else  if (idString == 19233){
                    intent.putExtra("idString", "richeditor-android");
                }else {
                    intent.putExtra("idString", idString+"");
                }
                String className = getItemDetailClassName();
                if (className != null) {
                    intent.putExtra("className", className);
                }
                String title = getDetailsToolBarTitle();
                if (title != null) {
                    intent.putExtra("title", getDetailsToolBarTitle());
                } else {
                    intent.putExtra("title", "开源中国");
                }
                startActivity(intent);
            }
        });
        
        return mAdapter;
    }

    protected abstract String getDetailsToolBarTitle();

    protected abstract String getItemDetailClassName();

    protected abstract int getUrlId();

    @Override
    protected void loadData() {
        int id = getUrlId();
        String url = String.format(Host.HOST_MY_Collection, id + "");
        HttpUtil<FavoriteList> httpUtil = new HttpUtil<>(getActivity(), FavoriteList.class);
        httpUtil.getStringData(url, new HttpUtil.OnGetDataListener<FavoriteList>() {
            @Override
            public void onSuccess(FavoriteList favoriteList) {
                bean = favoriteList;
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mAdapter.addDatas(bean.getFavoritelist());
                    }
                });
            }

            @Override
            public void onFailure(Exception e) {
                Log.d("test", "onFailure: " + e.getMessage());
            }
        });
    }
}
